/*
 * Copyright (C) 2008  Wang Hoi <fearee@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
*/
#include <QtNetwork>
#include "server.h"
#include "settings.h"
#include "http_source.h"
#include "buffer.h"

Server::Server(int argc, char *argv[]): Service(argc, argv)
{
}
Server::~Server()
{
    qDeleteAll(buffers.values());
    buffers.clear();
    qDebug() << "DeConstruct Server";
}
void Server::run()
{
    init();
    QCoreApplication::exec();
}
void Server::init()
{
    bool ok;
    Buffer *buffer;
    HttpSource *httpSrc;
    GetOpt opt(arguments());
    QString configFilePath;
    opt.addOption('f', "config", &configFilePath);
    ok = opt.parse();
    if (ok && configFilePath.size()) {
        qDebug() << QString("Loading config from %1").arg(configFilePath);
        settings = new Settings(configFilePath);
        foreach(QString key, settings->sources().keys()) {
            qDebug() << "Server::init():" << key << "--" << settings->sources().value(key);
            httpSrc = new HttpSource(settings->sources().value(key));
            buffer = new Buffer(key, httpSrc, this);
            buffers.insert(key, buffer);
        }
    } else {
        qFatal("no config files found, please specify one.");
    }
    server = new QTcpServer(this);
    connect(server, SIGNAL(newConnection()), SLOT(onNewConnection()));
    server->listen(settings->listenAddress(), settings->listenPort());
}
void Server::onNewConnection()
{
    QTcpSocket *socket = 0;
    Connection *conn = 0;
    while (socket = server->nextPendingConnection()) {
        conn = new Connection(socket, this);
        connect(conn, SIGNAL(disconnected()), SLOT(onConnDisconnected()));
        connect(conn, SIGNAL(error(QAbstractSocket::SocketError)),
                SLOT(onError(QAbstractSocket::SocketError)));
        conns << conn;
    }
}
void Server::onConnDisconnected()
{
    Connection *conn = qobject_cast<Connection *>(sender());
    if (conn) {
        conns.removeOne(conn);
        conn->deleteLater();
    }
}
void Server::onError(QAbstractSocket::SocketError)
{
    Connection *conn = qobject_cast<Connection *>(sender());
    if (conn) {
        conns.removeOne(conn);
        conn->deleteLater();
    }
}
Settings *Server::setting()
{
    return settings;
}
Buffer *Server::getBuffer(const QString &str)
{
    return buffers.value(str);
}
MemCache *Server::createMemCache()
{
    MemCache *mc;
    QList<QByteArray> servers;
    servers.clear();
    servers << "127.0.0.1:11211";
    mc = new MemCache();
    mc->setServers(servers);
    return mc;
}
FileCache *Server::createFileCache()
{
    FileCache *fc;
    fc = new FileCache();
    return fc;
}
#include "server.moc"

